package psk.lightshow;


import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Flash extends Activity{
	
private Button open; 	//开灯
private Button close;   //关灯
private Button blink;   //闪烁
private Camera camera;
private EditText rate;  //闪烁频率
int     b = 50;  
private Activity nowActivity = Flash.this;
        Handler myhandler = new Handler();
        Runnable light_blink_on = new Runnable() {		
	@Override
	public void run() {
	try{			
		camera = Camera.open();            
        Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);		
		}catch(Exception ex){
			ex.printStackTrace();
		}
		myhandler.postDelayed(light_blink_off, b);
	   }
};
        Runnable light_blink_off = new Runnable() {	
	@Override
	public void run() {
	try {
		Parameters parameters = camera.getParameters();
	    parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
	    camera.setParameters(parameters);
		camera.release();
		camera = null;			
		} catch (Exception ex) {
			ex.printStackTrace();			
		}
		myhandler.postDelayed(light_blink_on, b);
	    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.flash);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.head_back_title);
    initHead();
    initView();
}
private void initHead(){
	TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
    tvTitle.setText("闪光灯");
    Button btnBack = (Button)findViewById(R.id.btnBack);
    btnBack.setOnClickListener(new OnClickListener(){
    	public void onClick(View v){
    		nowActivity.finish();
    	}
    }); 
}
private void initView(){
    open   = (Button)findViewById(R.id.bt_on);
    close  = (Button)findViewById(R.id.bt_off);
    blink  = (Button)findViewById(R.id.bt_blink);
    rate   = (EditText)findViewById(R.id.et_blink);
 
    open.setOnClickListener(new openClickListener());
    close.setOnClickListener(new closeClickListener());
    blink.setOnClickListener(new blinkClickListener());   
}
//频闪
private class blinkClickListener implements OnClickListener{	
	@Override
	public void onClick(View arg0) {
	       if(rate.getText().toString().equals("")){
		     Toast.makeText(Flash.this, "input", Toast.LENGTH_SHORT).show();
			 return;
		}
	b = Integer.parseInt(rate.getText().toString());
	myhandler.post(light_blink_off);
	}
};
//开灯
private class openClickListener implements OnClickListener {	
	@Override
	public void onClick(View v) {
	try{			
		camera = Camera.open();            
        Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);		
		}catch(Exception ex){
			ex.printStackTrace();
		}
	   }
};
//关灯
private class closeClickListener implements OnClickListener {	
	@Override
	public void onClick(View v) {
	try {
		Parameters parameters = camera.getParameters();
		parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
		camera.setParameters(parameters);
		camera.release();
		camera = null;			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		myhandler.removeCallbacks(light_blink_on);
		myhandler.removeCallbacks(light_blink_off);
	}
};

}



	

